
var aMailServices = angular.module('ChunLe', ['ngRoute','ngAnimate','infinite-scroll']);



//配置路由
function emailRouteConfig($routeProvider) {

	
//首页
$routeProvider.when('/', {controller: MainController,templateUrl:'main.html'})

//店面
.when('/shop', {controller: ShopController,templateUrl:'shop.html'})

//套餐
.when('/package', {controller: PackageController,templateUrl:'package.html'})

//产品
.when('/goods/:goodId', {controller: GoodsController,templateUrl:'goods.html'})


//会员
.when('/user2', {controller: User2Controller,templateUrl:'user2.html'})

//会员资料编辑
.when('/user_info', {controller: UserInfoController,templateUrl:'user_info.html'})

//商城首页
.when('/market', {controller: MarketController, templateUrl:'market.html'})

//分类列表
.when('/category', {controller: CategoryController, templateUrl:'category.html'})

//产品列表
.when('/goods_list/:catId', {controller: GoodsListController, templateUrl:'goods_list.html'})

//搜索
.when('/goods_list/:catId', {controller: GoodsListController, templateUrl:'goods_list.html'})

//咨询列表
.when('/search_list/:searchName', {controller: GoodsListController, templateUrl:'goods_list.html'})

//咨询列表
.when('/article_info/:articleId', {controller: ArticleInfoController, templateUrl:'article_info.html'})

//咨询列表
.when('/article_list/:catId', {controller: ArticleListController, templateUrl:'article_list.html'})

//店面列表
.when('/shop_list', {controller: ShopListController, templateUrl:'shop_list.html'})

//店面地图列表
.when('/map_list', {controller: MapListController, templateUrl:'./map/baiduMap.html'})

//动态
.when('/trends', {controller: TrendsController, templateUrl:'trends.html'})

//我的收藏
.when('/collect', {controller: CollectController, templateUrl:'collect.html'})

//发布随手拍
.when('/publish', {controller: PublishController, templateUrl:'publish.html'})


//随手拍详情
.when('/picture_info/:newsId', {controller: PictureInfoController, templateUrl:'picture_info.html'})

.otherwise({redirectTo: '/'});

}


aMailServices.config(emailRouteConfig);


//根controller
function IndexController($scope,$rootScope,$location,$window) {
		$scope.showNull = false;
	 	$rootScope.changeNull = function(){
	 		$scope.showNull = true;
	 	}
	 	$rootScope.hideNull = function(){
	 		$scope.showNull = false;
	 	}
		$rootScope.hide = function(){
			retSetMenu();
		}
		
		
		//跳转地图
//		$rootScope.map = function(){
//			var url = "http://api.map.baidu.com/marker?location=";
//			var location = '23.477631,113.173263';
//			var content='地址：广州市白云区云城东路118号A10';
//			var title = '纯乐自造-迈家厨房-白云万达店';
//			//console.info(url+location+"&title="+title+"&content="+content+"&output=html");
//			window.location.href = url+location+"&title="+title+"&content="+content+"&output=html";
////			alert("aaa");
//			
//		}
		
		
		//店面列表切换标识
		$rootScope.config = {
				'map':'map'
		};
		
	   	//页面切换
		$rootScope.go = function (path, pageAnimationClass) {
	 	
		hideSpin();
		$rootScope.hideNull();
		
		//页面成功切换回调函数   main.js
		$rootScope.$on('$locationChangeSuccess', routeChangeSuccess); 
		
		//页面切换效果
	    if (typeof(pageAnimationClass) === 'undefined') { // Use a default, your choice
	        $rootScope.pageAnimationClass = 'slide';
	        
	    }
	         
	    else { // Use the specified animation
	        $rootScope.pageAnimationClass = pageAnimationClass;

	    }
	 
	    if (path === 'back') { // Allow a 'back' keyword to go to previous page
	        $window.history.back();
	    }
	         
	    else { // Go to the specified path
	    	
	    	
	    	//地图列表
	    	if($rootScope.config.map == "map" && path=="map")
    		{
	    		path="/map_list";
    		}
	    	//店面列表
	    	else if($rootScope.config.map == "shop" && path=="map")
    		{
	    		path="/shop_list";
    		}
	    	if($location.path()!=path){
	    		showSpin();
	    	}
	        $location.path(path);
	    }
	};
	//加上一个参数的方法 --页面切换
	$rootScope.goParameter = function (path, pageAnimationClass,parameter) {
 	
		hideSpin();
		$rootScope.hideNull();
		//页面成功切换回调函数   main.js
		$rootScope.$on('$locationChangeSuccess', routeChangeSuccess); 
		//页面切换效果
	    if (typeof(pageAnimationClass) === 'undefined') { // Use a default, your choice
	        $rootScope.pageAnimationClass = 'slide';
	    }else { // Use the specified animation
	        $rootScope.pageAnimationClass = pageAnimationClass;
	    }
	 
	    if (path === 'back') { // Allow a 'back' keyword to go to previous page
	        $window.history.back();
	    }else { // Go to the specified path
	    	//地图列表
	    	if($rootScope.config.map == "map" && path=="map")
			{
	    		path="/map_list"+"/"+parameter;
			}
	    	//店面列表
	    	else if($rootScope.config.map == "shop" && path=="map")
			{
	    		path="/shop_list"+"/"+parameter;
			}
	    	if($location.path()!=path){
	    		showSpin();
	    	}
	        $location.path(path+"/"+parameter);
	    }
};
	
}


//首页controller
function MainController($scope,$rootScope,$location,$http) {
	showSpin();
	bottomMenu();
	$scope.adUrl = ip+"/"+"data/afficheimg/";
	var url = cl.getMainInfoUrl+"&callback=JSON_CALLBACK";
    $http.jsonp(url).success(function (res) {
		if(res.code=="CL02020"){//获取成功
			$scope.background = res.result.background;
			$scope.top = res.result.top;
		}
		hideSpin();
    })
}


//店面controller
function ShopController($scope, $http) {
		//showSpin();
	hideSpin();
	rightMenu();
		
}


//套餐controller
function PackageController($scope, $http) {
		//showSpin();
	hideSpin();
	rightMenu();
		
}

//商品详情
aMailServices.factory('goods', function ($http) {
     var goods = function (goodId) {
    	 this.goodId = goodId;
    	 this.items =[];
    	 this.baseUrl = ip+"/";
         this.busy = false;
     };
     goods.prototype.getInfo = function(){
    	 if (this.busy){
        	 hideSpin();
        	 return;
         }
         this.busy = true;
    	 var url = cl.getGoodsInfoUrl+"&callback=JSON_CALLBACK&id="+this.goodId ;
         $http.jsonp(url).success(function (res) {
				if(res.code=="CL02013"){//获取成功
					this.items = res.result;
				}
				hideSpin();
				setTimeout("initBox()",1000);
				this.busy = false;
         }.bind(this));
     };
     return goods;
 });

//产品controller
function GoodsController($scope, $http,$sce,$routeParams,goods) {
	showSpin();
	rightMenu();
	$scope.goods = new goods($routeParams.goodId);
	$scope.goods.getInfo();
     
     $scope.deliberatelyTrustDangerousSnippet = function() {  
    	 
    	
    	 
    	 
    	 return $sce.trustAsHtml($scope.goods.items.goods_desc);  
    	 }; 
		
}

//首页工厂
aMailServices.factory('market', function ($http) {
     var market = function () {
    	 this.banners = [];
    	 this.catitems =[];
//    	 this.catitems =[{id:3,name:"烘焙主料"},{id:3,name:"烘焙辅料"},{id:3,name:"烘焙工具"},
//    	                 {id:3,name:"烘焙模具"},{id:3,name:"烘焙包材"},{id:3,name:"乳制品"},{id:3,name:"其他"}];
    	 this.hotitems = [];
    	 this.baseUrl = ip+"/";
    	 this.adUrl = ip+"/"+"data/afficheimg/";
         this.busy = false;
     };
     market.prototype.initMarket = function(){
    	 if (this.busy){
        	 hideSpin();
        	 return;
         }
         this.busy = true;
    	 var url = cl.initMarketUrl+"&callback=JSON_CALLBACK" ;
         $http.jsonp(url).success(function (res) {
				if(res.code=="CL02008"){//获取成功
					hideSpin();
					this.hotitems = res.result.hot_goods;
					this.banners = res.result.ad;
					this.catitems = res.result.cat_list;
				}
				setTimeout("initBox()",1000);
				
				this.busy = false;
         }.bind(this));
     };
     return market;
 });
//商城首页controller
function MarketController($scope, $routeParams,$rootScope,$location,market) {
	showSpin();
	
	bottomMenu();
	$scope.market = new market();
	$scope.market.initMarket();
}


//创建后台数据交互工厂
//aMailServices.factory('get_datas', function ($http) {
//     var get_datas = function () {
//         this.items = [];
//         this.busy = false;
//         this.page = 1;
//     };
//     get_datas.prototype.nextPage = function () {
//         if (this.busy) return;
//         this.busy = true;
//         var url = "http://app.weisai.net/shop.php?callback=JSON_CALLBACK&page="+ this.page ;
//        // var url = "http://192.168.3.95:7002/api/post/nextpage?id=" + this.page + "&callback=JSON_CALLBACK";
//         $http.jsonp(url).success(function (data) {
//        	//处理成功的情况
//				if(data.data.length!=0){
//		             var items = data.data;
//		             for (var i = 0; i < items.length; i++) {
//		                 this.items.push(items[i]);
//		             }
//		             this.busy = false;
//		             this.page += 1;
//				}
//		          hideSpin();
//		          
//		          setTimeout("loaded()",1000);
//         }.bind(this));
//     };
//     return get_datas;
// });

//会员资料编辑controller
function UserInfoController($scope, $routeParams,$rootScope,$location,wxUser) {
	showSpin();
	bottomMenu();
	$scope.item = cl.user;
	$scope.user = new wxUser();
	hideSpin();
	$scope.saveInfo = function(){
		showSpin();
		function success(){
			cl.user.initUser($scope.item);
			showAlert("","保存成功");
			$location.path("/user");
		}
		function fail(){
			showAlert("","保存失败");
		}
		if($scope.item.user_name!=""){
			$scope.user.save($scope.item,success,fail);
		}else{
			showAlert("","昵称不可为空");
			hideSpin();
		}
	}
}

//分类列表controller
function CategoryController($scope, $routeParams,$rootScope,$location) {
	hideSpin();
	bottomMenu();
	$scope.newlist = [1,2,3,4]
	
}
//创建后台数据交互工厂
aMailServices.factory('Goodslist', function ($http,$rootScope) {
     var Goodslist = function (cat_id,searchName) {
    	 this.cat_id= cat_id!=undefined?cat_id:"ALL";
    	 this.searchName= searchName!=undefined?searchName:"";
         this.items = [];
         this.busy = false;
         this.baseUrl = ip+"/";
         this.page = 1;
         this.order = "is_new";
     };
     
     Goodslist.prototype.nextPage = function () {
         if (this.busy) return;
         this.busy = true;
         var url = cl.getGoodsListUrl+"&callback=JSON_CALLBACK&page="+ this.page+"&cat_id="+this.cat_id+"&order=" +this.order+"&key="+this.searchName;
         $http.jsonp(url).success(function (res) {
        	 if(res.code=="CL02009"){
        		 if(res.result.goods_list.length!=0){
		             var items = res.result.goods_list;
		             for (var i = 0; i < items.length; i++) {
		                 this.items.push(items[i]);
		             }
		             this.page += 1;
				}
        	 }else if(res.code=="CL020010"){
        		 
        	 }
        	 if(this.items.length==0){
	      		   $rootScope.changeNull();
	      	  }else{
	      		  $rootScope.hideNull();
	      	  }
        	 this.busy = false;
             hideSpin();
         }.bind(this));
     };
 
     return Goodslist;
 });
//产品列表controller
function GoodsListController($scope, $routeParams,$rootScope,$location,$routeParams,Goodslist) {
	showSpin();
	rightMenu();
	$scope.Goodslist = new Goodslist($routeParams.catId,$routeParams.searchName);
	$scope.Goodslist.nextPage();
	$scope.order = function(order){
		showSpin();
		$scope.Goodslist.order = order;
		$scope.Goodslist.page = 1;
		$scope.Goodslist.items=[];
		$scope.Goodslist.nextPage();
	}
//	$scope.newlist = [1,2,3,4,5,6,7,8];
}


//纯乐课堂
aMailServices.factory('Articlelist', function ($http,$rootScope) {
     var Articlelist = function (cat_id) {
    	 this.cat_id= cat_id;
         this.items = [];
         this.busy = false;
         this.baseUrl = ip+"/";
         this.page = 1;
     };
     
     Articlelist.prototype.nextPage = function () {
         if (this.busy) return;
         this.busy = true;
         var url = cl.getArticleListUrl+"&callback=JSON_CALLBACK&page="+ this.page+"&cat_id="+this.cat_id ;
         $http.jsonp(url).success(function (res) {
        	 if(res.code=="CL02011"){
        		 if(res.result.article_list.length!=0){
        			// console.info(res.result.article_list);
		             var items = res.result.article_list;
		             for (var i = 0; i < items.length; i++) {
		                 this.items.push(items[i]);
		             }
		             this.page += 1;
				}
        	 }else if(res.code=="CL020012"){
        		 
        	 }
        	 if(this.items.length==0){
	      		   $rootScope.changeNull();
	      	  }else{
	      		  $rootScope.hideNull();
	      	  }
        	 this.busy = false;
             hideSpin();
         }.bind(this));
     };
 
     return Articlelist;
 });
//资讯列表controller
function ArticleListController($scope, $routeParams,$rootScope,$location,Articlelist,$routeParams) {
	showSpin();
	rightMenu();
	$scope.Articlelist = new Articlelist($routeParams.catId);
	$scope.Articlelist.nextPage();
	//$scope.newlist = [1,2,3,4];
}
//文章详情
aMailServices.factory('article', function ($http) {
     var article = function (articleId) {
    	 this.articleId = articleId;
    	 this.items =[];
    	 this.baseUrl = ip+"/";
         this.busy = false;
     };
     article.prototype.getInfo = function(){
    	 if (this.busy){
        	 hideSpin();
        	 return;
         }
         this.busy = true;
    	 var url = cl.getArticleInfoUrl+"&callback=JSON_CALLBACK&id="+this.articleId ;
         $http.jsonp(url).success(function (res) {
				if(res.code=="CL02015"){//获取成功
					this.items = res.result;
				}
				hideSpin();
				this.busy = false;
         }.bind(this));
     };
     return article;
 });
//资讯详情controller
function ArticleInfoController($scope, $http,$rootScope,$location,$sce,$routeParams,article) {
	
	showSpin();
	rightMenu();
	$scope.article =new article($routeParams.articleId);
	$scope.article.getInfo();
	
//	 var url = "http://app.weisai.net/mobile/art_info.php?act=detail&aid=1&callback=JSON_CALLBACK";
//     $http.jsonp(url).success(function (res) {
//    	 $scope.data = res.data;
//    	 hideSpin();
//    	
//     });
     
     $scope.deliberatelyTrustDangerousSnippet = function() {  
    	 return $sce.trustAsHtml($scope.article.items.content);  
    	 }; 
}

//纯乐课堂
aMailServices.factory('Maplist', function ($http,$location) {
     var Maplist = function (cat_id) {
    	 this.longitude = 0;
         this.latitude =0;
         this.speed = 0;
         this.accuracy = 0;
         //23.2040400000,113.2752080000
//         this.items=[{
//     		'id':4,'address':'广州市白云区荷塘月色小区星辉居EF107铺','tel':'13450358674','storeName':'纯乐自造-方圆店','longitude':113.275208,'latitude':23.204040
//     	},{
//     		'id':1,'address':'广州市白云区云城东路118号A10','tel':'020-36279767','storeName':'纯乐自造-白云万达店','longitude':113.278779,'latitude':23.185502
//     	},{
//     		'id':2,'address':'广州市白云区石槎路金碧新城24栋F39','tel':'020-36752296','storeName':'纯乐自造-金碧新城店','longitude':113.24929,'latitude':23.210387
//     	},{
//     		'id':3,'address':'广州市番禺区市桥西城路三街23号','tel':'18664828732','storeName':'纯乐自造-番禺市桥店','longitude':113.364138,'latitude':22.936614
//     	}];
         this.items=[];
         this.busy = false;
         
     };
     Maplist.prototype.getWxInfo = function(){
    	 if (this.busy) return;
    	 this.busy = true;
         var host = $location.host()+"%2Fhtml%2Fchunle%2Findex.html%23%2Fmap_list";
         var url = cl.getWxInfoUrl+"&callback=JSON_CALLBACK&host="+host;
         $http.jsonp(url).success(function (res) {
        	 var pro = this;
        	 if(res.code=="CL02017"){
        		 wx.config({
        			    debug: false, 
        			    appId: res.result.appId,
        			    timestamp: res.result.timestamp,
        			    nonceStr: res.result.nonceStr,
        			    signature: res.result.signature,
        			    jsApiList: [
        			      // 所有要调用的 API 都要加到这个列表中
        			      'getLocation',
        			    ]
        			  });
        			  wx.ready(function () {
        				  wx.getLocation({
        					    type: 'wgs84', // 默认为wgs84的gps坐标，如果要返回直接给openLocation用的火星坐标，可传入'gcj02'
        					    success: function (resu) {
        					    	pro.latitude = resu.latitude; // 纬度，浮点数，范围为90 ~ -90
        					    	cl.user.latitude = resu.latitude;
        					    	pro.longitude = resu.longitude; // 经度，浮点数，范围为180 ~ -180。
        					    	cl.user.longitude = resu.longitude;
        					    	pro.speed = resu.speed; // 速度，以米/每秒计
        					    	pro.accuracy = resu.accuracy; // 位置精度
        					    },
        					    complete : function(resu) {
        					    	pro.baiduMap();
        			            }
        					});
        			  });
        	 }
        	 this.busy = false;
         }.bind(this));
    	 
     }
     Maplist.prototype.baiduMap = function(){
    	 var pro = this;
    	 var address = this.items;
    	// 百度地图API功能
    		var map = new BMap.Map("l-map");
    		if(this.longitude==0&&this.latitude==0){
    			var point = new BMap.Point(address[0].longitude,address[0].latitude);
    			map.centerAndZoom(point, 11);
    		}else{
    			var mypoint = new BMap.Point(this.longitude,this.latitude);
        		var myIcon = new BMap.Symbol(BMap_Symbol_SHAPE_POINT, {
        	    scale: 0,//图标缩放大小
        	    fillColor: "blue",//填充颜色
        	    fillOpacity: 0.5//填充透明度
        	  });
        		map.centerAndZoom(mypoint, 11);
    		}
    		map.disableDoubleClickZoom();
    	  // 添加带有定位的导航控件
    	  var navigationControl = new BMap.NavigationControl({
    	    // 靠左上角位置
    	    anchor: BMAP_ANCHOR_TOP_LEFT,
    	    // LARGE类型
    	    type: BMAP_NAVIGATION_CONTROL_LARGE,
    	    // 启用显示定位
    	    enableGeolocation: true
    	  });
    	  map.addControl(navigationControl);
    	  if(this.longitude==0&&this.latitude==0){
    		  
    	  }else{
    		  var markerME = new BMap.Marker(mypoint,{icon:myIcon});  // 创建标注
    		  map.addOverlay(markerME);         // 将标注添加到地图中
    		  markerME.setLabel(new BMap.Label("所在位置",{offset:new BMap.Size(-16,-19)}));
    	  }
    	  // 添加定位控件
    	  var geolocationControl = new BMap.GeolocationControl();
    	  geolocationControl.addEventListener("locationSuccess", function(e){
    		  // 定位成功事件
    		  if(markerME){
    			  map.removeOverlay(markerME);
    		  }
    		  pro.latitude = e.point.lat;
    		  pro.longitude = e.point.lng;
    	  });
    	  geolocationControl.addEventListener("locationError",function(e){
    	    // 定位失败事件
    	  });
    	  map.addControl(geolocationControl);
    		var index = 0;
    		var myGeo = new BMap.Geocoder();
    		for(var i = 0; i<address.length; i++){
    			var marker = new BMap.Marker(new BMap.Point(address[i].longitude,address[i].latitude));
    			map.addOverlay(marker);
    			
    			marker.addEventListener("click",function(e){
    				$("#l-map").height("100%");
    				var p = e.target;
    				var add = getAdd(p.getPosition().lng,p.getPosition().lat,address);
    				showShop(add);
    				addEvent(address);
    			});
    		}
    		hideSpin();
     }
     Maplist.prototype.getMapList = function () {
         if (this.busy) return;
         this.busy = true;
         var url = cl.getMapListUrl+"&callback=JSON_CALLBACK";
         $http.jsonp(url).success(function (res) {
        	 this.busy = false;
        	 if(res.code=="CL02018"){
        		 this.items = res.result;
        		 if(!testProject){
            		 this.getWxInfo();//正式
            	 }else{
            		 this.baiduMap();//测试所用
            	 }
        	 }else if(res.code=="CL020019"){
        		 
        	 }
         }.bind(this));
     };
     return Maplist;
 });
//店面地图controller
function MapListController($scope, $routeParams,$rootScope,$location,Maplist) {
	mapMenu();
	$scope.list = new Maplist();
	//获取用户信息和店面信息
	$scope.list.getMapList();
	//测试所用
//	$scope.list.baiduMap();
//	$scope.list.baiduMap();
//	$scope.list.getWxInfo();
	$scope.visible = true;
	$scope.hideMapInfo = function()
	{
			$("#l-info").hide();
	}
	$rootScope.config.map = "shop";
	//$scope.newlist = [1,2,3,4];
}

//创建后台数据交互工厂
aMailServices.factory('shop', function ($http,$location,$rootScope) {
   var shop = function () {
	   this.longitude =0;
       this.latitude =0;
       this.speed = 0;
       this.accuracy = 0;
       //23.2040400000,113.2752080000
       this.items = [];
       this.busy = false;
       this.page = 1;
       this.adUrl = ip+"/"+"data/afficheimg/";
   };
//   shop.prototype.getWxInfo = function(){
//  	 if (this.busy) return;
//  	 this.busy = true;
//       var host = $location.host()+"%2Fhtml%2Findex.html%23%2Fshop_list";
//       var url = cl.getMapListUrl+"&callback=JSON_CALLBACK&host="+host;
//       $http.jsonp(url).success(function (res) {
//    	   this.busy = false;
//    	   var pro = this;
//      	 if(res.code=="CL02017"){
//      		 wx.config({
//      			    debug: false, 
//      			    appId: res.result.appId,
//      			    timestamp: res.result.timestamp,
//      			    nonceStr: res.result.nonceStr,
//      			    signature: res.result.signature,
//      			    jsApiList: [
//      			      // 所有要调用的 API 都要加到这个列表中
//      			      'getLocation',
//      			    ]
//      			  });
//      			  wx.ready(function () {
//      				  wx.getLocation({
//      					    type: 'wgs84', // 默认为wgs84的gps坐标，如果要返回直接给openLocation用的火星坐标，可传入'gcj02'
//      					    success: function (resu) {
//      					    	pro.latitude = Number(resu.latitude); // 纬度，浮点数，范围为90 ~ -90
//      					    	pro.longitude = Number(resu.longitude); // 经度，浮点数，范围为180 ~ -180。
//      					    	pro.speed = Number(resu.speed); // 速度，以米/每秒计
//      					    	pro.accuracy = Number(resu.accuracy); // 位置精度
//      					    	for( var o in pro.items){
//      							   if(pro.latitude==0&&pro.longitude==0){
//      								 pro.items[o].distance =parseInt("9999999");
//      							   }else{
//      								 pro.items[o].distance = parseInt(getFlatternDistance(pro.items[o].latitude,pro.items[o].longitude,pro.latitude,pro.longitude));//lat1,lng1,lat2,lng2
//      							   }
//      						   }
//      					    }
//      					});
//      			  });
//      	 }
//       }.bind(this));
//  	 
//   };
   shop.prototype.getDistance = function(items){
	   for( var o in items){
		   if(this.latitude==0&&this.longitude==0){
			   items[o].distance =parseInt(0);
			   items[o].distanceShow ="";
		   }else{
			   items[o].distance =parseInt(0);
			   items[o].distanceShow ="";
//			   this.items[o].distance = parseInt(getFlatternDistance(this.items[o].latitude,this.items[o].longitude,this.latitude,this.longitude));//lat1,lng1,lat2,lng2
//			   this.items[o].distanceShow = this.items[o].distanceShow+"m";
		   }
	   }
	   return items;
   };
   shop.prototype.nextPage = function () {
       if (this.busy) return;
       this.busy = true;
       var url = cl.getMapListPageUrl+"&callback=JSON_CALLBACK&page="+ this.page ;
       $http.jsonp(url).success(function (res) {
      	//处理成功的情况
    	   if(res.code=="CL02021"){
    		   if(res.result.length!=0){
		             var items = res.result;
		             items = this.getDistance(items);
		             for (var i = 0; i < items.length; i++) {
		                 this.items.push(items[i]);
		             }
		             this.page += 1;
				}
	      	}else if(res.code=="CL02022"){
	      		
	      	}
    	   if(this.items.length==0){
    		   $rootScope.changeNull();
    	   }else{
	      		  $rootScope.hideNull();
	      	  }
			this.busy = false;
	         hideSpin();
       }.bind(this));
   };
	shop.prototype.toAddress = function(add){
				var url = "http://api.map.baidu.com/marker?location=";
				var location = add.latitude+","+add.longitude;
			//	var location = '23.477631,113.173263';
				var content='地址：'+add.address;
				var title = add.storeName;
				window.location.href = url+location+"&title="+title+"&content="+content+"&output=html&src=chunle";
	}
   return shop;
});

//门店列表controller
function ShopListController($scope,$rootScope,shop){
	 showSpin();
	 mapMenu();
	 $rootScope.config.map = "map";
	 $scope.shop = new shop();
	 $scope.shop.longitude = Number(cl.user.longitude);
	 $scope.shop.latitude = Number(cl.user.latitude);
//	 $scope.shop.getWxInfo();
//	 $scope.shop.getDistance();
	 $scope.shop.nextPage();
	 $scope.distance = "distance";
}
/*
MapListController.resolve = {
		  delay: function($q) {
		    var delay = $q.defer(),
		    load = function(){
		    	$.getScript( "http://api.map.baidu.com/api?v=2.0&ak=b5dYoW2CbyGVaEQfXIm9dBdB" );
		    	$.getScript( "http://api.map.baidu.com/library/SearchInfoWindow/1.5/src/SearchInfoWindow_min.js" );
		    	$.getScript( "js/map.js",function(){
		        delay.resolve();
		        });
		      
		    };
		    load();
		    return delay.promise;
		  }
		}
*/






//发布controller
function PublishController($scope, $routeParams,$rootScope,$location,$http) {
	//showSpin();
	 
	 var url = cl.wxJssdkUrl+"&callback=JSON_CALLBACK&url=publish" ;
     $http.jsonp(url).success(function (res) {
    	 
    	 configWx(res);
			
     }.bind(this));

}

//新会员中心controller
function User2Controller($scope, $routeParams,$rootScope,$location,market) {
	//showSpin();
	
	//bottomMenu();
	
//	alert(111);
}



/**
 * 计算获取距离
 * approx distance between two points on earth ellipsoid
 * @param {Object} lat1
 * @param {Object} lng1
 * @param {Object} lat2
 * @param {Object} lng2
 */
function getFlatternDistance(lat1,lng1,lat2,lng2){
    var f = getRad((lat1 + lat2)/2);
    var g = getRad((lat1 - lat2)/2);
    var l = getRad((lng1 - lng2)/2);
    
    var sg = Math.sin(g);
    var sl = Math.sin(l);
    var sf = Math.sin(f);
    
    var s,c,w,r,d,h1,h2;
    var a = EARTH_RADIUS;
    var fl = 1/298.257;
    
    sg = sg*sg;
    sl = sl*sl;
    sf = sf*sf;
    
    s = sg*(1-sl) + (1-sf)*sl;
    c = (1-sg)*(1-sl) + sf*sl;
    
    w = Math.atan(Math.sqrt(s/c));
    r = Math.sqrt(s*c)/w;
    d = 2*w*a;
    h1 = (3*r -1)/2/c;
    h2 = (3*r +1)/2/s;
    return d*(1 + fl*(h1*sf*(1-sg) - h2*(1-sf)*sg));
}
var EARTH_RADIUS = 6378137.0;    //单位M
var PI = Math.PI;

function getRad(d){
    return d*PI/180.0;
}
function showAlert(msg){
	alert(msg);
}